package ejemplo;

import java.io.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.util.regex.Pattern;

public class Add extends HttpServlet {

    private AccesoDatos ad;
    private ResultSet rs;
    private MyTableModel mtb;
    private String referer, mensaje;
   
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {} 

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {} 

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
         RequestDispatcher rd;
         ServletContext context = getServletContext();
         InputStream header = context.getResourceAsStream("/header.xhtml");
         response.setContentType("text/html;charset=UTF-8");
         PrintWriter out = response.getWriter();
         if(request.getParameter("referer") != null)
             referer=request.getParameter("referer");
         String table=request.getParameter("table");
         try {
            if (header != null) {
                InputStreamReader isr = new InputStreamReader(header);
                BufferedReader reader = new BufferedReader(isr);
                String line ="";
                if(table.equals("libros")){
                    while ((line = reader.readLine()) != null){
                        if(Pattern.matches(".*OPER.*", line))
                            out.print(line.replaceFirst("OPER", "Agregar libro"));
                        else out.print(line);
                    }
                } //endif table == libros
                else if(table.equals("temas")){
                    while ((line = reader.readLine()) != null){
                        if(Pattern.matches(".*OPER.*", line))
                            out.print(line.replaceFirst("OPER", "Agregar tema"));
                        else out.print(line);
                    }
                } //endif table == temas
            } //endif if (header != null)
            // consulta y cuerpo

            if(table.equals("libros")){
                ad = new AccesoDatos("gencod");
                rs = ad.getAllTemas();
                mtb = new MyTableModel(rs);

               //leer plantilla
               InputStream abmc_libro = context.getResourceAsStream("/abmc-libro.xhtml");
               if (abmc_libro != null) {
                   InputStreamReader isr = new InputStreamReader(abmc_libro);
                   BufferedReader reader = new BufferedReader(isr);
                   String line ="";
                   while ((line = reader.readLine()) != null){
                       if(Pattern.matches(".*ACTION.*", line))
                           out.println(line.replaceFirst("ACTION", "\"./insert\""));
                       else if(Pattern.matches(".*VALUE1.*", line))
                           out.println(line.replaceFirst("VALUE1", "\"\""));
                       else if(Pattern.matches(".*VALUE2.*", line))
                           out.println(line.replaceFirst("VALUE2", "\"\""));
                       else if(Pattern.matches(".*VALUE3.*", line))
                           out.print(line.replaceFirst("VALUE3", "\"\""));
                       else if(Pattern.matches(".*VALUE4.*", line))
                           out.print(line.replaceFirst("VALUE4", "\"\""));
                       else if(Pattern.matches(".*SELECT.*", line)){
                           out.println(line.replaceFirst("SELECT", ""));
                           for (int row = 0; row < mtb.getRowCount(); row++){
                                out.print("<option value=\"");
                                out.print(mtb.getValueAt(row, 0));
                                out.println("\">" + mtb.getValueAt(row, 1) + "</option>");
                           } //endfor
                       } //endif MATCHES SELECT
                      else if(Pattern.matches(".*BUTTON1.*", line))
                           out.println(line.replaceFirst("BUTTON1", "\"Agregar\""));
                      else if(Pattern.matches(".*BACK.*", line))
                           out.println(line.replaceFirst("BACK", "\"./" + referer + "\""));
                      else out.println(line);
                   } //endwhile
               } //endif if (abmc_libro != null)
            } //endif(table.equals("libros")){
            else if(table.equals("temas")){
              //leer plantilla
              InputStream abmc_tema = context.getResourceAsStream("/abmc-tema.xhtml");
              if (abmc_tema != null) {
                InputStreamReader isr = new InputStreamReader(abmc_tema);
                BufferedReader reader = new BufferedReader(isr);
                String line ="";
                while ((line = reader.readLine()) != null){
                  if(Pattern.matches(".*ACTION.*", line))
                    out.println(line.replaceFirst("ACTION", "\"./insert\""));
                  else if(Pattern.matches(".*VALUE1.*", line))
                    out.println(line.replaceFirst("VALUE1", "\"\""));
                  else if(Pattern.matches(".*BUTTON1.*", line))
                    out.println(line.replaceFirst("BUTTON1", "\"Agregar\""));
                  else if(Pattern.matches(".*BACK.*", line))
                    out.println(line.replaceFirst("BACK", "\"./" + referer + "\""));
                  else out.println(line);
                } //endwhile
               } //endif if (abmc_tema != null)
            } //endif(table.equals("temas"))
            mensaje=(String)request.getAttribute("mensaje");
            if(mensaje != null){
              out.println("<center><h2>");
              out.println("<font color=\"#FFFFFF\">");
              out.println(mensaje);
              out.println("</font></h2></center>");
            }       
         } catch(ClassNotFoundException cnfe){
           rd=request.getRequestDispatcher("/index");
           mensaje="Error fatal. Excepcion de clase no encontrada: " + cnfe.getMessage();
           request.setAttribute("mensaje",mensaje);
           rd.forward(request,response);
         } catch(SQLException sqle){
           rd=request.getRequestDispatcher("/index");
           mensaje="Error fatal. Excepcion de SQL: " + sqle.getErrorCode();
           request.setAttribute("mensaje",mensaje);
           rd.forward(request,response);
         } 
         finally {   out.close();   }
    }

    @Override
    public String getServletInfo(){ return "Short description"; }
}